
import { _decorator, Component, Vec2, Vec3, PhysicsSystem2D, RaycastResult2D } from 'cc';
import { ColliderTag } from './Globle';
import { MoveObject } from './MoveObject';
import { Player } from './Player';
const { ccclass } = _decorator;

@ccclass('People')
export class People extends MoveObject {
    private offsetPath: Vec2[] = [];
    private speed: number = 1;
    /**
     * 初始化
     * init
     */
    public init(offsetPath?: Vec2[]) {
        if (offsetPath) this.offsetPath = offsetPath;
    }

    //射线检测
    onRayCast(rays: Readonly<RaycastResult2D[]>[]) {
        for (let i of rays) {
            if (!i || !i.length) continue;
            for (let ray of i) {
                switch (ray.collider.tag) {
                    case ColliderTag.player:
                        ray.collider.node.getComponent(Player).fireLvUp(1);
                        this.node.destroy();
                        break;
                }
            }

        }
    }

    update(deltaTime: number) {
        super.update(deltaTime);
        if (!this.offsetPath.length) return;
        let vec2_pos = new Vec2(this.node.position.x, this.node.position.y);
        let vec = this.offsetPath[0].clone().subtract(vec2_pos);
        let direction = vec.clone().normalize();
        this.node.position = new Vec3(vec2_pos.x + direction.x * this.speed, vec2_pos.y + direction.y * this.speed);
        let distance = vec.length();
        if (distance <= 1) this.offsetPath.shift();
    }
}
